home *** CD-ROM | disk | FTP | other *** search
- {$D-,R-,S+}
-
- UNIT StrObj;
-
- {Linked list holds strings.
-
- Version 1.00: released to the public domain on 28 August 1989.
-
- Version 1.01: released to the public domain on 1 September 1989.
- The TPRO5 conditional compilation directive has been removed. The code is
- just as fast with TPRO5 as it is without it.
- Added a call to the standard procedure FAIL inside the Init() constructor
- in case there isn't enough memory to add a new string to the heap.
- Added new method that returns length of the string. Much faster than just
- checking the length with LENGTH(GetString).}
-
-
- INTERFACE {section}
-
- USES
- Objects;
-
-
- TYPE
- StrObjectPtr = ^StrObject;
- StrObject
- = OBJECT(Node)
- TheStrPtr : POINTER;
-
- CONSTRUCTOR Init(NewStr : STRING);
- DESTRUCTOR Done; VIRTUAL;
-
- FUNCTION GetString : STRING;
- FUNCTION GetStringLength : BYTE;
- PROCEDURE ChangeString(NewStr : STRING);
- FUNCTION SelfPtr : StrObjectPtr;
- END;
-
-
- IMPLEMENTATION {section}
-
-
- {============================================================================}
- CONSTRUCTOR StrObject.Init(NewStr : STRING);
-
- {Initialize the StrObject. ...I originally used SUCC(LENGTH(NewStr)) to
- determine the size of the true string array, but I forgot that SUCC returns
- a value of the same type as the variable you sent it. That's bad news if you
- try to put a 255-char string on the heap! Think about it.}
-
- VAR
- TrueStrSize : WORD;
-
- BEGIN {StrObject.Init}
- TrueStrSize := (LENGTH(NewStr) + 1); {must account for 0th byte!}
-
- IF (MAXAVAIL < TrueStrSize)
- THEN
- FAIL
- ELSE
- BEGIN
- GETMEM({VAR} TheStrPtr,TrueStrSize);
- MOVE(NewStr,TheStrPtr^,TrueStrSize)
- END
- END; {StrObject.Init}
- {============================================================================}
-
- {============================================================================}
- DESTRUCTOR StrObject.Done;
-
- {De-initialize the StrObject. See my comments in the Init routine!}
-
- VAR
- TrueStrSize : WORD;
-
- BEGIN {StrObject.Done}
- TrueStrSize := (BYTE(TheStrPtr^) + 1);
- FREEMEM(TheStrPtr,TrueStrSize)
- END; {StrObject.Done}
- {============================================================================}
-
- {============================================================================}
- FUNCTION StrObject.GetString : STRING;
-
- {Return the Str from the object.}
-
- BEGIN {GetString}
- GetString := STRING(TheStrPtr^)
- END; {GetString}
- {============================================================================}
-
- {============================================================================}
- FUNCTION StrObject.GetStringLength : BYTE;
-
- {Return the length of the Str from the object. Much faster than calling
- LENGTH(GetString).}
-
- BEGIN {GetString}
- GetStringLength := BYTE(TheStrPtr^)
- END; {GetString}
- {============================================================================}
-
- {============================================================================}
- PROCEDURE StrObject.ChangeString(NewStr : STRING);
-
- {Changes the Str in the object to the NewStr.}
-
- BEGIN {ChangeString}
- Done;
- Init(NewStr)
- END; {ChangeString}
- {============================================================================}
-
- {============================================================================}
- FUNCTION StrObject.SelfPtr : StrObjectPtr;
-
- {Returns a pointer to this object.}
-
- BEGIN {SelfPtr}
- SelfPtr := @Self
- END; {SelfPtr}
- {============================================================================}
-
-
- END. {StrObj}